<?php

/**
 *   OPENSHOP 尺码相关设置
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: size_lib.php,v 1.0 2009/06/12 07:35:58 modified $
 *   @author      FillBag <fillbag@hotmail.com>
 */

define('IN_OS', true);

require_once(dirname(__FILE__).'/includes/init.php');
$exc = new exchange($os->table('size_lib'),$db,'lib_id','lib_name');

/* act操作项的初始化 */
if (empty($_REQUEST['act'])){
    $_REQUEST['act'] = 'list';
}else{
    $_REQUEST['act'] = trim($_REQUEST['act']);
}

if($_REQUEST['act']=='list'){
	
	//检查权限
	admin_priv();
	
	$smarty->assign('ur_here',      '尺码对照表');  
	$smarty->assign('action_link',  array('href' => 'size_lib.php?act=add', 'text' => '添加尺码对照表'));
	$smarty->assign('full_page',    1);
	$size_lib_list = get_sizelib_list();
	$smarty->assign('size_lib_list',    $size_lib_list['size_lib']);
    $smarty->assign('filter',       $size_lib_list['filter']);
    $smarty->assign('record_count', $size_lib_list['record_count']);
    $smarty->assign('page_count',   $size_lib_list['page_count']);	

    assign_query_info();
	
	$smarty->display('size_lib_list.tpl');	
}elseif($_REQUEST['act']=='add' || $_REQUEST['act']=='edit'){   
    $is_add = $_REQUEST['act'] == 'add';
    
    /* 权限检查 */
    $priv_str = $is_add ? 'add_size_lib' : 'edit_size_lib';
    admin_priv($priv_str);
    
    $smarty->assign('form_act', $is_add ? 'insert' : 'update');
    
     /* 初始化、取得尺码对照表信息 */
    if ($is_add)
    {
        $size_lib = array(
            'lib_id'        => 0,
            'lib_name'      => '',
            'lib_desc'      => '',
            'is_define'      => 1,
            'site_url'  => ''
        );
    }
    else
    {
        if (empty($_GET['lib_id']))
        {
            sys_msg('invalid param');
        }
        $lib_id = intval($_GET['lib_id']);
        $size_lib = $db->getRow("SELECT * FROM ".$os->table('size_lib')." WHERE lib_id='$lib_id'");
        if (empty($size_lib))
        {
            sys_msg('您要操作的尺码对照表不存在');
        }
    }
    $smarty->assign('size_lib', $size_lib);
       
	include_once(ROOT_PATH . 'includes/fckeditor/fckeditor.php'); // 包含 html editor 类文件
	
    /* 创建 html editor */
    create_html_editor('lib_desc', $size_lib['lib_desc']);
    
    /* 显示模板 */
    if ($is_add)
    {
        $smarty->assign('ur_here', '添加尺码对照表');
    }
    else
    {
        $smarty->assign('ur_here', '编辑尺码对照表');
    }
    $href = 'size_lib.php?act=list';
    if (!$is_add)
    {
        $href .= '&' . list_link_postfix();
    }
    $smarty->assign('action_link', array('href' => $href, 'text' => '尺码对照表管理'));
    	
	assign_query_info();
	$smarty->display('size_lib_info.tpl');
		
}elseif($_REQUEST['act']=='insert' || $_REQUEST['act'] == 'update'){    
     /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'insert';
    
    /* 权限检查 */
    $priv_str = $is_add ? 'add_size_lib' : 'edit_size_lib';
    admin_priv($priv_str);
        
    /* 检查名称是否重复 */
    $lib_name = sub_str($_POST['lib_name'], 50, false);
    if (!$exc->is_only('lib_name', $lib_name,intval($_POST['lib_id'])))
    {
       sys_msg(sprintf("尺码对照表名称 %s 已经存在！", stripslashes($lib_name)), 1);
    } 
    
    /* 提交值 */
	$size_lib = array(
		'lib_id'        => intval($_POST['lib_id']),
		'lib_name'      => $lib_name,
		'lib_desc'      => $_POST['lib_desc'],
		'is_define'     => $_POST['is_define'],
		'site_url'      => $_POST['site_url']
	);
    /* 保存数据 */
    if ($is_add)
    {
        $db->autoExecute($os->table('size_lib'), $size_lib, 'INSERT');
        $size_lib['lib_id'] = $db->insert_id();
    }
    else
    {
        $db->autoExecute($os->table('size_lib'), $size_lib, 'UPDATE', "lib_id = '$size_lib[lib_id]'");
    }  
    if ($is_add){
        admin_log($size_lib['lib_name'], 'add', 'size_lib');
    }else{
        admin_log($size_lib['lib_name'], 'edit', 'size_lib');
    }

    /* 清除缓存 */
    clear_cache_files();
    
    /* 提示信息 */
    if ($is_add){
        $links = array(
            array('href' => 'size_lib.php?act=add', 'text' => '继续添加尺码对照表'),
            array('href' => 'size_lib.php?act=list', 'text' => '返回尺码对照表列表')
        );
        sys_msg('添加尺码对照表成功', 0, $links);
    }else{
        $links = array(
            array('href' => 'size_lib.php?act=list&' . list_link_postfix(), 'text' => '返回尺码对照表列表')
        );
        sys_msg('编辑尺码对照表成功', 0, $links);
    } 
}elseif ($_REQUEST['act']=='remove'){
	
	/* 检查权限 */
    check_authz_json('drop_size_lib');

    $lib_id = intval($_GET['id']);
    $size_lib = $db->getRow("SELECT * FROM ".$os->table('size_lib')." WHERE lib_id='$lib_id'");
    if (empty($size_lib))
    {
        make_json_error('您要操作的尺码对照表不存在');
    }
    $goods_size_lib_count = $db->getOne('SELECT COUNT(*) FROM ' .$os->table('goods'). " WHERE size_lib_id='$lib_id'");
    if($goods_size_lib_count==0){
	    /* 删除该颜色 */
	    if($exc->drop($lib_id)){
		    /* 记日志 */
		    admin_log($size_lib['lib_name'], 'remove', 'size_lib');   	
	    }
    }else{
    	make_json_error('"'.$size_lib['lib_name'] .'" 该尺码对照表与商品还有关联,您不能删除!');
    }

    /* 清除缓存 */
    clear_cache_files();

    $url = 'size_lib.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

    os_header("Location: $url\n");
    exit;	
}elseif($_REQUEST['act']=='query'){
    $list = get_sizelib_list();

    $smarty->assign('size_lib_list',  $list['size_lib']);
    $smarty->assign('filter',          $list['filter']);
    $smarty->assign('record_count',    $list['record_count']);
    $smarty->assign('page_count',      $list['page_count']);
    
    make_json_result($smarty->fetch('size_lib_list.tpl'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));	
}

function get_sizelib_list(){
    $result = get_filter();
    if ($result === false){
        /* 分页大小 */
        $filter = array();

        $filter['keyword']  = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);

        if ($_REQUEST['is_ajax'] == 1)
        {
            $filter['keyword'] = json_str_iconv($filter['keyword']);
        }        
        
        if ($filter['keyword'])
        {
			$where .= " AND (lib_name LIKE '%" . mysql_like_quote($filter['keyword']) . "%' OR lib_desc LIKE '%" . mysql_like_quote($filter['keyword']) . "%')";
        }           
        
        /* 记录总数以及页数 */
        $sql = "SELECT COUNT(*) FROM ".$GLOBALS['os']->table('size_lib').' where 1 '.$where;
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        $filter = page_and_size($filter);
        
        /* 查询记录 */
		$sql = "SELECT lib_id,lib_name,lib_desc,is_define,site_url FROM ". $GLOBALS['os']->table('size_lib') .' where 1 '.$where." ORDER BY lib_id DESC";

        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);
    
    $arr = array();
    while ($rows = $GLOBALS['db']->fetchRow($res))
    {
        $arr[] = $rows;
    }	
    
    return array('size_lib' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);	
}
?>